<template>
  <div class="top-category">
    <div class="container">
      <!-- 面包屑 -->
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <Transition name="fade-right" mode="out-in">
          <XtxBreadItem>{{topCategory.name}}</XtxBreadItem>
        </Transition>
      </XtxBread>
      <!-- 轮播图 -->
      <XtxCarousel autoPaly :sliders="sliders" style="height:500px"></XtxCarousel>
      <!-- 全部分类 -->
      <div class="sub-list">
        <h3>全部分类</h3>
        <ul>
          <li v-for="sub in topCategory.children" :key='sub.id'>
            <a href="javascript:;">
              <img :src="sub.picture">
              <p>{{sub.name}}</p>
            </a>
          </li>
        </ul>
      </div>
      <!-- 各个类别推荐商品 -->
      <div class="ref-goods" v-for="sub in subList" :key='sub.id'>
        <div v-if="sub.goods.length>0">
          <div class="head">
            <h3>- {{sub.name}} -</h3>
            <p class="tag">温暖柔软，品质之选</p>
            <XtxMore class="xtx-more" :path="`/category/sub/${sub.id}`"></XtxMore>
          </div>
          <div class="body">
            <goods-item v-for="good in sub.goods" :key="good.id" :goods="good"></goods-item>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import GoodsItem from './components/goods-item.vue'
import { findBanner } from '@/api/home'
import { ref } from '@vue/reactivity'
import { useStore } from 'vuex'
import { useRoute } from 'vue-router'
import { computed, watch } from '@vue/runtime-core'
import { findTopCategory } from '@/api/category.js'
export default {
  name: 'TopCategory',
  components: { GoodsItem },
  setup () {
    // 轮播图
    const sliders = ref([])
    findBanner().then(data => {
      sliders.value = data.result
    })
    // 面包屑+所有子分类(数据都在vuex中)
    const store = useStore()
    // 依赖于路径上的分类id
    const route = useRoute()
    console.log(store.state.category.category)
    console.log(route.params.id)
    const topCategory = computed(() => {
      // 当前顶级分类===根据路由上的id去vuex中category模块中的list查找
      // console.log(route.params.id)
      // console.log(store.state.category.category)
      let cate = {}
      const item = store.state.category.category.find(item => {
        return item.id === route.params.id
      })
      if (item) cate = item
      return cate
    })

    // 获取各个子类目下的推荐商品
    const subList = ref([])
    const getSubList = () => {
      findTopCategory(route.params.id).then(data => {
        subList.value = data.result.children
      })
    }
    // 监听到地址栏上的id变化，就更新数据
    watch(() => route.params.id, (newVal) => {
      // newVal && getSubList()
      // 避免点击二级类目时重复发起请求（在顶级类名下才发请求）
      if (newVal && `/category/${newVal}` === route.path) getSubList()
    }, { immediate: true })

    return { sliders, topCategory, subList }
  }
}
</script>

<style lang="less" scoped>
.top-category{
  h3{
    font-size: 28px;
    color:#666;
    font-weight: normal;
    text-align: center;
    line-height: 100px;
  }
  .sub-list{
    margin-top: 20px;
    background-color: #fff;
    ul{
      display: flex;
      padding: 0 32px;
      flex-wrap: wrap;
      li{
        width: 168px;
        height: 160px;
        a{
          text-align: center;
          display: block;
          font-size: 16px;
          img{
            width: 100px;
            height: 100px;
          }
          p{
            line-height: 40px;
          }
          &:hover{
            color:@xtxColor;
          }
        }
      }
    }
  }
  .ref-goods{
    background-color: #fff;
    margin-top: 20px;
    position: relative;
    .head{
      // position: relative;
      .xtx-more{
        position:absolute;
        top:20px;
        right:20px;
      }
      .tag{
        text-align: center;
        color: #999;
        font-size: 20px;
        position: relative;
        top:-20px;
      }
    }
    .body{
      display: flex;
      justify-content: flex-start;
      flex-wrap: wrap;
      padding:0 65px 30px;
    }
  }
}
</style>
